#include "FieldGenerator.h"

#include "HeadFile.h"
#include "Token.h"
#include "MetaGenerator.h"
#include "TypeGenerator.h"

CFieldGenerator::CFieldGenerator(CHeadFile *InContext)
{
    Context = InContext;
    Result = std::make_shared<CFieldInfo>();
}

void CFieldGenerator::Generate()
{
    //skip RFIELD
    {
        auto RFIELDToken = Context->GetCurrentToken();
        if(RFIELDToken->Type == ETokenType::RFIELD)
        {
            Context->GotoNextToken();
            Result->ErrorToken=RFIELDToken;
        }
        else
        {
            Context->RaiseError(RFIELDToken, U"expect 'RFIELD'");
            return ;
        }
    }

    //read meta
    {
        auto MetaGenerator = std::make_shared<CMetaGenerator>(Context);
        MetaGenerator->Generate();
        Result->Meta = MetaGenerator->Result;
    }
    
    // if(Result->Meta.contains(u8"Name"))
    // {
    //     Result->IsVirtual = true; 
    //     return ;
    // }

    //read all token until ';' or '='
    {
        std::vector<std::shared_ptr<CToken>> Tokens;

        {
            auto Token = Context->GetCurrentToken();
            while(Token->Type != ETokenType::Semicolon && Token->Type != ETokenType::Assign)
            {
                Tokens.push_back(Token);

                Context->GotoNextToken();
                Token = Context->GetCurrentToken();
            }



        }

        Result->Name = Tokens.back()->ToString().ToUtf8();
        Tokens.pop_back();

        auto TypeGenerator = std::make_shared<CTypeGenerator>(Context, Tokens);
        TypeGenerator->Generate();
        Result->Type = TypeGenerator->Result;
        Result->IsReadOnly = TypeGenerator->bConst;


    }


}
